<template>
  <div class="menu-wrapper" v-if="routers">
    <div class="menu-title">
      <span class="iconfont icon-gouwu"></span>
      <span class="title-text">Maos</span>
    </div>
    <div class="content">
      <div class="person-info">
        <img :src="userAvatar" alt="" />
        <div class="person-name">
          <div class="name">MAOS</div>
          <span class="detail">test</span>
        </div>
      </div>
      <div class="menu-content">
        <div class="menu-list">
          <div class="menu-list-item" @click="setActive(item.path)" :class="{ 'active': activeItem === item.path }"
            v-for="item in routers" :key="item.path">
            <div class="block"></div>
            <span class="iconfont" :class="item.meta.icon"></span>
            <div class="item-name">{{ item.meta.title }}</div>
          </div>
          <div class="menu-list-item">
            <div class="block"></div>
            <span class="iconfont icon-tuichudenglu"></span>
            <div class="item-name" @click="logout">退出登录</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
const route = useRoute()
const router = useRouter();
const storage = useUserStore();

const routers = computed(() => router.getRoutes().filter(el => el.meta.showInMenu))
const activeItem = ref(route.path)
const userAvatar = computed(() => `${url()}/images/92790d4f-1476-4d31-a4fa-2b43b1e204a5.jpg`)

watch(
  () => route.path,
  (newPath) => {
    activeItem.value = newPath;
  }
);

const setActive = (item) => {
  activeItem.value = item;
  return navigateTo(item)
};
const logout = async () => {
  try {
    // 先清除本地存储和状态
    const token = useCookie('X-Auth-Token')
    token.value = null
    storage.token = ''
    $message('成功退出', 'success')
    // 然后调用登出接口
    await navigateTo('/', {
      replace: true,
    })
  } catch (error) {
    console.error('登出失败:', error)
    $message('登出失败，请重试', 'error')
  }
}


onMounted(() => {
  document.body.setAttribute("theme-color", "light");
})
</script>

<style scoped lang="scss">
.active {
  background-color: var(--theme-hover-menu-color);

  .item-name {
    color: var(--theme-hover-color);
  }

  .iconfont {
    color: var(--theme-hover-color);
  }

  .block {
    opacity: 1;
  }
}


.iconfont {
  font-style: normal;
  font-size: 25px;
  color: black;
  vertical-align: middle;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.menu-wrapper {
  width: 90px;
  border-radius: 20px;
  background-color: var(--bg-color);
  padding: 20px;
  box-sizing: border-box;
  transition: 0.6s;
  overflow: hidden;

  .menu-title {
    padding-bottom: 20px;
    box-sizing: border-box;
    border-bottom: 1px solid rgb(229, 233, 236);
    margin-bottom: 20px;
    display: flex; // 添加 flex 布局
    align-items: center; // 垂直居中对齐

    .title-text {
      margin-left: 10px;
      font-family: "BasketBall";
      font-size: 30px;
      opacity: 0;
      visibility: hidden; // 改用 visibility 替代 opacity
      width: 0; // 收缩时宽度为 0
      transition: 0.6s;
      color: var(--theme-info-text-color);
    }

    .icon-24gf-bag {
      font-size: 30px;
      padding-left: 10px;
      color: rgb(103, 56, 223);
    }
  }

  .content {


    .person-info {
      margin-top: 20px;
      white-space: nowrap;

      img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        border: 2px solid #fff;
        box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
        vertical-align: middle;
      }

      .person-name {
        margin-left: 15px;
        vertical-align: middle;
        opacity: 0;
        transition: 0.6s;
        overflow: hidden;
        color: var(--theme-info-text-color);
        display: inline-block;

        .name {
          font-size: 20px;
          font-weight: 600;
        }

        .detail {
          font-size: 12px;
        }
      }
    }

    .menu-content {
      margin-top: 20px;

      .menu-list {
        .menu-list-item {
          cursor: pointer;
          width: 100%;
          height: 50px;
          font-size: 18px;
          position: relative;
          border-radius: 10px;
          padding-left: 10px;
          white-space: nowrap;

          .block {
            width: 6px;
            height: 25px;
            background: rgb(101, 57, 225);
            position: absolute;
            right: -10px;
            top: 13px;
            transition: 0.5s;
            border-top-left-radius: 4px;
            border-bottom-left-radius: 4px;
            opacity: 0;
          }

          .item-name {
            line-height: 50px;
            display: inline-block;
            margin-left: 10px;
            font-size: 18px;
            color: var(--theme-text-color);
            font-weight: 100;
            transition: 0.6s;
            opacity: 0;
          }



          .btn {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            display: inline-block;
            opacity: 0;
            margin-left: 10px;

            .check-ipt {
              display: none;
            }

            .check-lable {
              display: inline-block;
              width: 40px;
              height: 20px;
              border-radius: 20px;
              background: rgb(101, 58, 223);
              /* border: 1px solid #eee; */
              cursor: pointer;
              position: relative;
              overflow: hidden;
            }

            .check-lable::before {
              display: inline-block;
              content: "";
              width: 15px;
              height: 15px;
              border-radius: 50%;
              background: white;
              position: absolute;
              left: 3px;
              top: 50%;
              transform: translateY(-50%);
              transition: all 0.3s;
            }

            .check-ipt:checked+.check-lable::before {
              left: 20px;
              background: rgb(32, 30, 43);
            }

            .check-ipt:checked+.check-lable::after {
              width: 100%;
            }
          }
        }
      }
    }
  }

  &:hover {
    width: 220px;

    .menu-title {
      .title-text {
        opacity: 1;
        visibility: visible; // 显示文本
        width: auto; // 恢复自然宽度
      }
    }

    .person-info {
      .person-name {
        opacity: 1;
      }
    }

    .menu-content {
      .menu-list {
        .menu-list-item {
          .item-name {
            opacity: 1;
          }

          .btn {
            opacity: 1;
          }
        }
      }
    }
  }
}
</style>
